package com.example.onlinetaxi.BaiDuMap.RoutePlan;

import static com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnPolylineClickListener;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLngBounds;
import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.example.onlinetaxi.LocalStroage.PassengerRouteLine;

import java.util.ArrayList;
import java.util.List;

public abstract class OverlayManager implements OnMarkerClickListener, OnPolylineClickListener {

    // 百度地图
    BaiduMap mBaiduMap = null;
    // 覆盖物选项
    private List<OverlayOptions> mOverlayOptionList = null;
    // 覆盖物列表
    List<Overlay> mOverlayList = null;

    // 初始化
    public OverlayManager(BaiduMap baiduMap) {
        mBaiduMap = baiduMap;
        // 初始化覆盖物列表
        if (mOverlayOptionList == null) {
            mOverlayOptionList = new ArrayList<OverlayOptions>();
        }
        if (mOverlayList == null) {
            mOverlayList = new ArrayList<Overlay>();
        }
    }

    // 添加路线
    public abstract List<OverlayOptions> drawLine(DrivingRouteLine drivingRouteLine, String type);

    // 添加起点和终点
    public abstract List<OverlayOptions> drawPoint(DrivingRouteLine drivingRouteLine, String type);

    // 绘制小车并且进行移动
    public abstract void drawCar();

    // 在地图上添加覆盖物(乘客起点到终点的路线)
    public final void addToMap(DrivingRouteLine drivingRouteLineForPassenger) {
        if (mBaiduMap == null) {
            return;
        }

        // 先清除已经绘制的
        removeFromMap();

        // 绘制普通路线
        mOverlayOptionList.addAll(drawLine(drivingRouteLineForPassenger, "passenger"));
        // 绘制起点和终点
        mOverlayOptionList.addAll(drawPoint(drivingRouteLineForPassenger, "both"));

        // 将所有的标记物全部添加在图上
        for (OverlayOptions option : mOverlayOptionList) {
            mOverlayList.add(mBaiduMap.addOverlay(option));
        }
    }

    // 在地图上添加覆盖物(乘客起点到终点的路线)
    public final void addCarToMap(DrivingRouteLine drivingRouteLineForPassenger) {
        if (mBaiduMap == null) {
            return;
        }

        // 先清除已经绘制的
        removeFromMap();

        // 绘制起点和终点
        mOverlayOptionList.addAll(drawPoint(drivingRouteLineForPassenger, "both"));

        // 绘制司机路线
        mOverlayOptionList.addAll(drawLine(drivingRouteLineForPassenger, "driver"));
        // 在地图上绘制小车
        drawCar();

        // 将所有的标记物全部添加在图上
        for (OverlayOptions option : mOverlayOptionList) {
            mOverlayList.add(mBaiduMap.addOverlay(option));
        }
        // 获取指定的标记物
        for (Overlay overlay : mOverlayList) {
            if (overlay.getExtraInfo() != null) {
                if (overlay.getExtraInfo().getString("type").equals("driver")) {
                    // 不显示这一段路程，因为之前已经绘制过
                    overlay.setVisible(false);
                }
            }
        }
    }

    // 在地图上添加覆盖物
    public final void addToMap(DrivingRouteLine drivingRouteLineForPassenger, DrivingRouteLine drivingRouteLineForDriver) {
        if (mBaiduMap == null) {
            return;
        }

        // 先清除已经绘制的
        removeFromMap();

//        mOverlayOptionList.addAll(drawLine(drivingRouteLineForPassenger, "passenger"));  // 绘制路线1
        mOverlayOptionList.addAll(drawPoint(drivingRouteLineForPassenger, "start"));  // 绘制路线1的起点

        mOverlayOptionList.addAll(drawLine(drivingRouteLineForDriver, "driver"));  // 绘制路线2
        // 在地图上绘制小车
        drawCar();


        // 将所有的标记物全部添加在图上
        for (OverlayOptions option : mOverlayOptionList) {
            mOverlayList.add(mBaiduMap.addOverlay(option));
        }
        // 获取指定的标记物
        for (Overlay overlay : mOverlayList) {
            if (overlay.getExtraInfo() != null) {
                if (overlay.getExtraInfo().getString("type").equals("driver")) {
                    // 不显示这一段路程，因为之前已经绘制过
                    overlay.setVisible(false);
                }
            }
        }
    }

    // 清除所有覆盖物
    public final void removeFromMap() {
        if (mBaiduMap == null) {
            return;
        }
        for (Overlay marker : mOverlayList) {
            marker.remove();
        }
        mOverlayOptionList.clear();
        mOverlayList.clear();

    }

    // 设置显示在规定宽高中的地图地理范围
    public void zoomToSpanPaddingBounds(int paddingLeft, int paddingTop, int paddingRight,
                                        int paddingBottom) {
        if (mBaiduMap == null) {
            return;
        }
        // 通过起点和终点定位缩放
        if (PassengerRouteLine.overlays.size() > 0) {

            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            for (Overlay overlay : PassengerRouteLine.overlays) {
                // polyline 中的点可能太多，只按marker 缩放
                if (overlay instanceof Marker) {
                    builder.include(((Marker) overlay).getPosition());
                }
            }

            mBaiduMap.setMapStatus(MapStatusUpdateFactory
                    .newLatLngBounds(builder.build(), paddingLeft, paddingTop, paddingRight,
                            paddingBottom));
        } else if (mOverlayList.size() > 0) {
            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            for (Overlay overlay : mOverlayList) {
                // polyline 中的点可能太多，只按marker 缩放
                if (overlay instanceof Marker) {
                    builder.include(((Marker) overlay).getPosition());
                }
            }

            mBaiduMap.setMapStatus(MapStatusUpdateFactory
                    .newLatLngBounds(builder.build(), paddingLeft, paddingTop, paddingRight,
                            paddingBottom));
        }
    }
}
